﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using Database.Context;
using Database.Models;
using QuanLyThongTinCoSo.Common;

namespace QuanLyThongTinCoSo.Biz
{
    public class HangMucBiz : BaseBiz
    {
        public HangMucBiz()
        {
            DbContext = new Context();
        }

        public HangMucBiz(Context context)
        {
            DbContext = context;
        }

        public override void Insert(BaseModel model)
        {
            var hangMuc = model as HangMuc;
            hangMuc.Created = DateTime.Now;
            hangMuc.Modified = DateTime.Now;
            hangMuc.CreatedBy = Utilities.CurrentUser();
            hangMuc.ModifiedBy = Utilities.CurrentUser();

            DbContext.HangMucs.Add(hangMuc);
            DbContext.SaveChanges();
        }

        public override void Update(BaseModel model)
        {
            var item = model as HangMuc;
            item.Modified = DateTime.Now;
            item.ModifiedBy = Utilities.CurrentUser();
            DbContext.Entry(item).State = (EntityState)System.Data.EntityState.Modified;
            DbContext.SaveChanges();
        }

        public override void Delete(BaseModel model)
        {
            DbContext.HangMucs.Attach(model as HangMuc);
            DbContext.HangMucs.Remove(model as HangMuc);
            DbContext.Entry(model).State = EntityState.Deleted;
            DbContext.SaveChanges();
        }

        public override void DeleteItems(List<BaseModel> models)
        {
            throw new System.NotImplementedException();
        }

        public override BaseModel GetItemById(int id)
        {
            return (from item in DbContext.HangMucs
                    where item.Id == id
                    select item).First();
        }

        #region GetAll

        public List<HangMuc> GetAll()
        {
            return DbContext.HangMucs.OrderBy(p => p.TenCongTrinh).ToList();
        }

        #endregion
    }
}
